<?php
/**
 * Created by PhpStorm.
 * User: bzyy
 * Date: 2015/7/4
 * Time: 15:38
 *
 * 检测每个队列的状态
 *
 *
 */


session_start();

require_once './class/defind.php';
require_once './class/redis.php';
require_once './class/bzyy.php';
require_once './class/class_mysql.php';


$key = isset($_REQUEST['key'])?$_REQUEST['key']:'';
if(empty($key))
{
    ajax_return(FLAG_ERROR,'未找到对应的key值');
}


$redis = init_redis();
$flag_data = $redis->get($key);
if(empty($flag_data))
{
    ajax_return(FLAG_ERROR,'key值无效,可能状态超时');
}

if($flag_data == QUEUE_OK)//处理成功，请找到订单数据
{
    $mysql = init_mysql();
    //找到订单数据
    $sql = "SELECT * FROM `order` WHERE  `key` = '{$key}'";
    $order_info = $mysql->getRow($sql);
    $redis->remove($key);
    if(empty($order_info))
    {
        ajax_return(FLAG_ERROR,'数据异常',array('key'=>$key));
    }
    ajax_return(FLAG_OK,'请去支付吧',$order_info);

}

if($flag_data == QUEUE_NO_GOODS)//库存不足了，其他的处理返回
{
    $redis->remove($key);
    ajax_return(FLAG_ERROR,'商品已抢光');
}

//更新时间戳
$redis->set($key,time());
ajax_return(AJAX_QUEUE_WAITTING,'处理中 ...',array('key'=>$key));
